/**
 * 
 */
package soa.security.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.ContainerCriteria;
import org.springframework.ldap.query.SearchScope;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.stereotype.Repository;

import soa.security.dao.GroupDao;
import soa.security.domain.Group;

/**
 * @author Cre.Gu
 *
 */
@Repository
public class GroupDaoImpl extends SpringLdapDaoImpl<Group> implements GroupDao {
	@Resource
	private LdapTemplate ldapTemplate;

	@Override
	public void create(Group t) {
		t.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
		ldapTemplate.create(t);
	}

	@Override
	public Group findByDn(String dn) {
		Group o = ldapTemplate.findByDn(
				LdapNameBuilder.newInstance(dn).build(), Group.class);
		o.setBid(LdapUtils.getStringValue(o.getDn(), o.getDn().size() - 1));
		o.setOrganization(unitBelongs(dn));
		return o;
	}

	@Override
	public List<Group> allGroups(String baseDn, SearchScope searchScope,
			Map<String, Object> parameters) {
		ContainerCriteria query = baseQuery(baseDn, searchScope);

		buildLikeCriteria(query, parameters, "cname");

		return ldapTemplate.search(query, new BaseContextMapper());
	}

	@Override
	public String getObjectClass() {
		return "zjtzsw-group";
	}
}
